package cn.ichiva;

import cn.ichiva.common.R;
import cn.ichiva.common.ThreadContent;
import cn.ichiva.common.jwt.JWTParser;
import cn.ichiva.service.*;
import cn.ichiva.vo.ApiVO;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Controller
public class Api {

    private final RedisService redisService;
    private final JWTParser jwtParser;

    public Api(RedisService redisService, JWTParser jwtParser) {
        this.redisService = redisService;
        this.jwtParser = jwtParser;
    }

    @ResponseBody
    @PostMapping("/api")
    public R<?> post(@RequestHeader("token") String token, @RequestBody ApiVO vo){
        try {
            String parse = jwtParser.parse(token);
            String account = JSON.parseObject(parse).getString("account");
            ThreadContent.put("account",account);
            return R.success(redisService.exec(vo));
        }finally {
            ThreadContent.clear();
        }
    }
}
